#ifndef NAVIGATION_H
#define NAVIGATION_H

#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QHostInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
#include <QMainWindow>
#include <QMessageBox>
#include <QMutexLocker>
#include <QNetworkAccessManager>
#include <QNetworkInterface>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPainter>
#include <QTimer>
#include <QUrl>
#include <QtMath>
#include <QtXml>
#include "dijkstra.h"
#include "geograph.h"

namespace Ui {
class Navigation;
}

class Navigation : public QWidget
{
    Q_OBJECT

public:
    explicit Navigation(QWidget *parent = nullptr);
    ~Navigation();

    struct coordinatesStr //对应的经纬度坐标(QString)
    {
        int index;
        QString lon;
        QString lat;
    };
    QMap<QString, coordinatesStr> nodeStrmap;
    QMap<int, coordinatesStr> nodeIndexMap;
    struct road_data
    {
        QString road_id;
        QList<QString> node_id;
    };
    QList<road_data> roadlist;

private slots:
    //处理获取外网ip请求的 槽
    void onGetIp(QNetworkReply *);
    //根据ip获取经纬度  处理服务器响应内容
    void onGetCurrentLoc(QNetworkReply *);
    //处理服务器返回地图图片
    void onSendMapRequest();

    //搜索地址 处理响应内容
    void onSearchLoc(QNetworkReply *);

    void on_enlargeBtn_7_clicked();
    void on_reduceBtn_7_clicked();

    void on_selectBtn_7_clicked(bool checked);
    void on_stop_selecBtn_7_clicked(bool checked);
    void on_backBtn_7_clicked(bool checked);

    void on_startpointBtn_7_clicked(bool checked);

    void on_endpointBtn_7_clicked(bool checked);

    void on_clearBtn_7_clicked(bool checked);

    void on_finishBtn_7_clicked(bool checked);

    void on_navigateBtn_7_clicked(bool checked);

    void on_alterpointBtn_7_clicked(bool checked);

    void on_continueBtn_7_clicked(bool checked);
    //搜索地址 发送请求
    void on_searchBtn_7_clicked(bool checked);

private:
    Ui::Navigation *ui;

    QNetworkAccessManager *m_ipManager;
    QNetworkAccessManager *m_locManager;
    QNetworkAccessManager *m_searchManager;
    //网络管理对象  发送地图图片
    QNetworkAccessManager *m_mapManager;
    //响应对象 接收地图图片
    QNetworkReply *m_mapReply = NULL;

    //初始化
    void init();
    //baidu App key
    QString ak = "UnYBybF9JfVau0PPDrXMqU0U3R31XGL3";
    //"yYSnHDIgAbSRlyktehYXmvlzA0s3eEhX" "UnQkg4ppkGWfRdJQR3kDqSoosraSVyaj";
    //存储当前主机ip
    QString currentIp;
    //经度
    double m_lng;
    //纬度
    double m_lat;
    //控制好地图的精度  高清或低清[3-18]
    int m_zoom = 12;
    //bit路网数据
    QString m_paths;
    //当前所在的城市
    QString m_city;
    //地图文件名
    QString m_mapFileName = "map.png";
    QTimer m_timer;
    QFile mapFile;
    double min_lon, max_lon;
    double min_lat, max_lat;
    //记录路径总数
    int road_num = 0;
    //记录标记点
    QString m_note;
    //记录起点和终点的坐标
    double startpoint_lon, startpoint_lat;
    double endpoint_lon, endpoint_lat;
    int start_index, end_index;
    //记录当前最短路长度
    double min_dist;
    //记录当前最短路
    QVector<int> min_dist_paths;

    //是否按下鼠标按钮
    bool isPress = false;
    //是否释放鼠标按钮
    bool isRelease = false;
    //按下鼠标的坐标点
    QPoint startPoint;
    //释放鼠标的坐标点
    QPoint endPoint;
    //x轴距离差
    double mx;
    //y轴距离差
    double my;
    //是否在选择阶段
    bool isSelecting = false;
    bool isSelectingStartPoint = false;
    bool haveSelectedStartPoint = false, haveSelectedEndPoint = false;
    //状态flag
    bool isSearching = false;
    bool isCalculating = true;
    bool isNavigating = false;
    bool isPaused = false;
    //动态轨迹
    QList<QPointF> path_points;
    QTimer *timer;    // Timer controlling the animation
    int currentRoadIndex; // The current index in the path_points
    qreal step = 0.01;       // The current step between points
    QPointF currentPos;

    //获取当前主机ip
    void getIp();
    //根据ip获取经纬度  发送请求
    void getCurrentLoc();
    // 发送请求  获取地图图片
    void sendMapRequest();
    //过滤事件
    bool eventFilter(QObject *watched, QEvent *event);
    //读入xml文件
    void readXMLFiles();
    //坐标转换
    double getPiclon(double lon);
    double getPiclat(double lat);
    double getPic_definite_lon(double lon);
    double getPic_definite_lat(double lat);
    double getReal_lon(double lon);
    double getReal_lat(double lat);
    //绘图
    void paintRoads();
    void updateAnimation();

    //求最短路
    void getMinDistance();
    double getRealDistance();
    //转图
    void getMinPath(
        int start_index, int end_index, double s_lon, double s_lat, double e_lon, double e_lat);
    QPair<int, int> findNearPoint(double s_lon, double s_lat, double e_lon, double e_lat);
};

#endif // NAVIGATION_H
